package machine;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.JTextComponent;

class MemoryGUI extends JPanel
    implements ListSelectionListener, ActionListener
{

    private MainMemory theMem;
    private JList memList;
    private JScrollPane scrollPane;
    private JPanel mbPan;
    private JTextField mbField;
    JComboBox displayOptions;
    ListModel memData;

    public MemoryGUI(MainMemory mainmemory)
    {
        memData = ((ListModel) (new AbstractListModel() {

            public int getSize()
            {
                if(theMem != null)
                {
                    return theMem.getMem().size() / 4;
                } else
                {
                    return 1;
                }
            }

            public Object getElementAt(int i)
            {
                if(theMem != null)
                {
                    String s1 = theMem.read(Integer.toBinaryString(i * 4));
                    String s = s1.substring(0, 4) + " " + s1.substring(4, 8) + " " + s1.substring(8, 12) + " " + s1.substring(12, 16) + " " + s1.substring(16, 20) + " " + s1.substring(20, 24) + " " + s1.substring(24, 28) + " " + s1.substring(28, 32);
                    s = s + "  | ";
                    String s2 = "" + i * 4;
                    for(int j = 0; j < 6 - s2.length(); j++)
                    {
                        s = s + " ";
                    }

                    s = s + s2;
                    return ((Object) (s));
                } else
                {
                    return ((Object) ("0000 0000 0000 0000 0000 0000 0000 0000  |      0"));
                }
            }

        }));
        theMem = mainmemory;
        ((Container)this).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (this)), 1))));
        ((Container)this).add(((Component) (new JLabel("  "))));
        ((Container)this).add(((Component) (new JLabel("Memory Browser"))));
        memList = new JList(memData);
        memList.setPrototypeCellValue("0000 0000 0000 0000 0000 0000 0000 0000  | 999999");
        memList.setVisibleRowCount(10);
        memList.setSelectionMode(0);
        memList.addListSelectionListener(((ListSelectionListener) (this)));
        ((JComponent) (memList)).setAutoscrolls(true);
        scrollPane = new JScrollPane();
        scrollPane.getViewport().setView(((Component) (memList)));
        ((Container)this).add(((Component) (scrollPane)));
        mbPan = new JPanel();
        ((Container) (mbPan)).add(((Component) (new JLabel("Value: "))));
        mbField = new JTextField(20);
        ((Container) (mbPan)).add(((Component) (mbField)));
        ((Container)this).add(((Component) (mbPan)));
        String as[] = {
            "Signed", "Unsigned", "Hexadecimal", "Machine Language"
        };
        displayOptions = new JComboBox(((Object []) (as)));
        ((JComponent) (displayOptions)).setFont(new Font("Monospaced", 0, 10));
        displayOptions.addActionListener(((ActionListener) (this)));
        ((Container)this).add(((Component) (displayOptions)));
        ((JComponent) (memList)).revalidate();
        ((JComponent) (scrollPane)).revalidate();
        ((Component) (scrollPane)).repaint();
    }

    public void setMem(MainMemory mainmemory)
    {
        theMem = mainmemory;
        ((JComponent) (memList)).revalidate();
        ((JComponent) (scrollPane)).revalidate();
        ((Component) (scrollPane)).repaint();
    }

    public void valueChanged(ListSelectionEvent listselectionevent)
    {
        set();
    }

    private void set()
    {
        if(theMem != null)
        {
            String s = (String)displayOptions.getSelectedItem();
            String s2 = theMem.read(Integer.toBinaryString(memList.getSelectedIndex() * 4));
            String s1;
            if(s.equals("Signed"))
            {
                Long long1 = Long.valueOf(s2, 2);
                s1 = (new Integer((int)long1.longValue())).toString();
            } else
            if(s.equals("Unsigned"))
            {
                Long long2 = Long.valueOf(s2, 2);
                s1 = long2.toString();
            } else
            if(s.equals("Hexadecimal"))
            {
                Long long3 = Long.valueOf(s2, 2);
                s1 = Long.toHexString(long3.longValue());
                s1 = "0x" + s1.toUpperCase();
            } else
            if(s.equals("Machine Language"))
            {
                s1 = theMem.getMLComment(Integer.toBinaryString(memList.getSelectedIndex() * 4));
                if(s1 == null)
                {
                    s1 = theMem.getAsmComment(Integer.toBinaryString(memList.getSelectedIndex() * 4));
                }
            } else
            {
                s1 = "Crap!";
            }
            ((JTextComponent) (mbField)).setText(s1);
        }
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        set();
    }

    public void update()
    {
        ((JComponent) (memList)).revalidate();
        ((JComponent) (scrollPane)).revalidate();
        ((Component) (scrollPane)).repaint();
        ((Component) (memList)).repaint();
    }

    

}
